﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using TestServices.Model;

namespace TestServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MainService" in code, svc and config file together.
    [ServiceContract]
    ////[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class MainService
    {
        private string content;
        private TestServices.Model.ChatAppEntities _chatAppEntities;

        public MainService()
        {
            _chatAppEntities = new ChatAppEntities();
        }

        public void DoWork()
        {
        }

        [OperationContract]
        public string GetContent()
        {
            return content;
        }

        [OperationContract]
        public void SetContent(string cont)
        {
            this.content = cont;
        }

        [OperationContract]
        public User CreateNewUser(string userName, string pass)
        {
            User user = new User()
                            {
                                CreatedTime = DateTime.Now,
                                LastAccessTime = DateTime.Now,
                                Password = pass,
                                UserName = userName,
                                UserPK = Guid.NewGuid()
                            };
            this._chatAppEntities.Users.AddObject(user);
            this._chatAppEntities.SaveChanges();
            return user;
        }

        [OperationContract]
        public User CheckLogin(string userName, string pass)
        {
            var user = this._chatAppEntities.Users.SingleOrDefault(x => x.UserName == userName && x.Password == pass);

            if (user != null)
            {
                user.LastAccessTime = DateTime.Now;
                user.IsOnline = true;
                this._chatAppEntities.SaveChanges();
            }
            return user;
        }

        [OperationContract]
        public void UpdateOfflineUser(Guid userPK)
        {
            var user = this._chatAppEntities.Users.SingleOrDefault(x => x.UserPK == userPK);

            if (user != null)
            {
                user.IsOnline = false;
                this._chatAppEntities.SaveChanges();
            }
        }

        [OperationContract]
        public List<User> GetFriend(Guid currentUserPK)
        {
            return this._chatAppEntities.Users.Where(x => x.UserPK != currentUserPK).ToList();

        }

        [OperationContract]
        public void CreateNewChatContent(Guid user1PK, Guid user2PK, string chatStrings)
        {
            Guid chatContentPK = Guid.NewGuid();
            ChatContent chatContent = new ChatContent()
            {
                ChatContentPK = chatContentPK,
                ChatString = chatStrings
            };

            this._chatAppEntities.ChatContents.AddObject(chatContent);


            this._chatAppEntities.SaveChanges();
            User_ChatContent userChatContent = new User_ChatContent()
                                                   {
                                                       User1FK = user1PK,
                                                       User2FK = user2PK,
                                                       ChatContentFK = chatContentPK,
                                                       UserChatContentPK = Guid.NewGuid()
                                                   };
            this._chatAppEntities.User_ChatContent.AddObject(userChatContent);


            this._chatAppEntities.SaveChanges();
        }

        [OperationContract]
        public void UpdateChatContent(Guid user1PK, Guid user2PK, string chatStrings)
        {
            var userChatContent =
                this._chatAppEntities.User_ChatContent.SingleOrDefault(x => x.User1FK == user1PK && x.User2FK == user2PK);
            if (userChatContent == null)
            {
                userChatContent =
            this._chatAppEntities.User_ChatContent.SingleOrDefault(x => x.User2FK == user1PK && x.User1FK == user2PK);
                var chatContent = this._chatAppEntities.ChatContents.SingleOrDefault(x => x.ChatContentPK == userChatContent.ChatContentFK);
                chatContent.ChatString = chatStrings;
            }
            else
            {
                var chatContent = this._chatAppEntities.ChatContents.SingleOrDefault(x => x.ChatContentPK == userChatContent.ChatContentFK);
                chatContent.ChatString = chatStrings;
            }

            this._chatAppEntities.SaveChanges();
        }

        [OperationContract]
        public string GetChatContent(Guid user1PK, Guid user2PK)
        {
            var chatContentPK =
               this._chatAppEntities.User_ChatContent.SingleOrDefault(x => x.User1FK == user1PK && x.User2FK == user2PK);
            if (chatContentPK == null)
            {
                chatContentPK =
                 this._chatAppEntities.User_ChatContent.SingleOrDefault(x => x.User1FK == user2PK && x.User2FK == user1PK);
                if (chatContentPK == null)
                {
                    return string.Empty;
                }
                else
                {
                    var chatContent =
                        this._chatAppEntities.ChatContents.SingleOrDefault(
                            x => x.ChatContentPK == chatContentPK.ChatContentFK);
                    return chatContent.ChatString;
                }
            }
            else
            {
                var chatContent =
                    this._chatAppEntities.ChatContents.SingleOrDefault(
                        x => x.ChatContentPK == chatContentPK.ChatContentFK);
                return chatContent.ChatString;
            }
        }


    }
}
